<?php
	class PdoMysql{
		public $pdo;
		function __construct($dbname,$dbhost,$dbuser,$dbpw){
			$url = "mysql:dbname={$dbname};host={$dbhost};";
			try{
				$this->pdo = new PDO($url,$dbuser,$dbpw);
				$this->pdo->exec("set names 'utf8'");
			}catch(PDOException $e){
				echo $e->getMessage();
			}
		}
		public function dbInsert($table,$name,$values,$nopre=0){
			$names = "";
			$val = "";
			if($nopre==0){
				$num = 1;
				foreach($name as $key){
					$names .= "`{$key}`,";
					$val .= '?,';
				}
				$names = substr($names,0,strlen($names)-1);
				$val = substr($val,0,strlen($val)-1);
				$query = "INSERT INTO {$table}({$names}) VALUES({$val})";
				$stmt = $this->pdo->prepare($query);
				$values = array_values($values);
				for($i=0;$i<count($values);$i++){
					$stmt->bindParam($num,$values[$i],$this->select_datatype($wValues[$i]));
					$num++;
				}
				$stmt->execute();
			}else{
				foreach($name as $key){
					$names .= "`{$key}`,";
				}
				foreach($values as $key){
					$val .= "'{$key}',";
				}
				$names = substr($names,0,strlen($names)-1);
				$val = substr($val,0,strlen($val)-1);
				$sql = "INSERT INTO {$table}({$names}) VALUES({$val})";
				$this->pdo->exec($sql);
			}
		}
		public function dbUpdate($table,$name,$values,$where,$wValues,$nopre=0){
			$set = "";
			if($nopre==0){
				$num = 1;
				for($i=0;$i<count($name);$i++){
					$set = $set."`{$name[$i]}`=?,";
				}
				$values = array_values($values);
				$set = substr($set,0,strlen($set)-1);
				$query = "UPDATE {$table} SET {$set} WHERE {$where}";
				$stmt = $this->pdo->prepare($query);
				if(is_array($values)){
					for($i=0;$i<count($values);$i++){
						$stmt->bindParam($num,$values[$i],$this->select_datatype($wValues[$i]));
						$num++;
					}
				}
				if(is_array($wValues)){
					for($i=0;$i<count($wValues);$i++){
						$stmt->bindParam($num,$wValues[$i],$this->select_datatype($wValues[$i]));
						$num++;
					}
				}
				$stmt->execute();
			}else{
				for($i=0;$i<count($name);$i++){
					$set = $set."`{$name[$i]}`='{$values[$i]}',";
				}
				$set = substr($set,0,strlen($set)-1);
				$sql = "UPDATE {$table} SET {$set} WHERE {$where}";
				$this->pdo->exec($sql);		
			}
		}
		public function dbDelete($table,$where,$wValues){
			$num = 1;
			$query = "DELETE FROM `{$table}` WHERE {$where}";
			$stmt = $this->pdo->prepare($query);
			if(is_array($wValues)){
				for($i=0;$i<count($wValues);$i++){
					$stmt->bindParam($num,$wValues[$i],$this->select_datatype($wValues[$i]));
					$num++;
				}
			}
			$stmt->execute();
		}
		public function query($sql){
			$st = $this->pdo->query($sql);
			$rs = $st->fetchAll();
			return $rs;
		}
		public function exec($sql){
			$this->pdo->exec($sql);
			return true;
		}
		public function dbSelect($table,$where='',$wValues=0,$iscount=0){
			$resArray = array();
			$num = 1;
			if(strlen($where)!=0){
				if($iscount==0)
					$query = "SELECT * FROM `{$table}` WHERE {$where}";
				else
					$query = "SELECT count(*) FROM `{$table}` WHERE {$where}";
			}
			else{
				if($iscount==0)
					$query = "SELECT * FROM `{$table}`";
				else
					$query = "SELECT count(*) FROM `{$table}`";
			}
			$stmt = $this->pdo->prepare($query);
			if(is_array($wValues)){
				for($i=0;$i<count($wValues);$i++){
					$stmt->bindParam($num,$wValues[$i],$this->select_datatype($wValues[$i]));
					$num++;
				}				
			}

			$result = $stmt->execute();
			$data = $stmt->fetchAll();
			
			foreach($data as $key){
				array_push($resArray,$key);
			}	
			return $resArray;
		}
		private function select_datatype($val) {
			if (is_bool($val)) {
				return PDO::PARAM_BOOL;
			} elseif (is_int($val)) {
				return PDO::PARAM_INT;
			} elseif (is_null($val)) {
				return PDO::PARAM_NULL;
			} else {
				return PDO::PARAM_STR;
			}
		}
	}

	$mysql = new PdoMysql($_config['db']['dbname'],$_config['db']['dbhost'],$_config['db']['dbuser'],$_config['db']['dbpw']);
?>